package com.sgb.kjwl.view.ui.activity.enterprise;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDealedCasesListBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.enterprise.DealedCasesEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.DealedCasesAdapter;
import com.sgb.kjwl.viewmodel.enterprise.DealedCasesModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class DealedCasesListActivity extends AppActivity {

    @Inject
    DealedCasesModel model;
    private ActivityDealedCasesListBinding binding;

    DealedCasesAdapter adapter;
    long serviceId = 0;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder().appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_dealed_cases_list);
        binding.include.titleView.setText("成交案例");
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(view -> finish());
        serviceId = getIntent().getLongExtra("serviceId", 0);


        if (adapter == null)
            adapter = new DealedCasesAdapter(this, null);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.displayView.setLayoutManager(layoutManager);
        binding.displayView.setAdapter(adapter);
        binding.displayView.setLoadingMoreEnabled(true);
        binding.displayView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                model.getCasesQueryList(false, serviceId);
            }

            @Override
            public void onLoadMore() {
                model.getCasesQueryList(true, serviceId);
            }
        });

    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model.getCasesQueryList(false, serviceId);
    }

    /**
     * 杰出人才数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showList(List<DealedCasesEntity> list, boolean isLoadMore) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
                adapter.addDatas(list);
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            binding.displayView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.displayView.setVisibility(View.VISIBLE);
                mErrorView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.displayView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatas(null);
                binding.displayView.setVisibility(View.GONE);
                mErrorView.setVisibility(View.VISIBLE);
            }
        }
    }
}
